﻿using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using Method = SampleMethod;

    public static class SampleMethodExtension
    {
        public static List<Method> getMethodList(this SampleMethod method, CoreLimsContext dbContext) => dbContext.Method.ToList();

        public static Method getMethod(this SampleMethod method, CoreLimsContext dbContext, string Id) =>
            dbContext.Method.FirstOrDefault(s => s.Id == Id);

        public static List<Method> getMethodListByType(this Method method, CoreLimsContext dbContext, string Type)
        {
            if (Type == "全部")
                return dbContext.Method.OrderBy(x => x.Code).ToList();

            return dbContext.Method.Where(s => s.Type == Type).OrderBy(x => x.Code).ToList();
        }

        public static string AddMethod(this Method method, CoreLimsContext dbContext)
        {
            method.Id = SAMPLE._newId();
            method.Status = Common.Status.草稿.ToString();
            method.Enable = true;

            dbContext.Add(method);
            dbContext.SaveChanges();
            return "方法添加成功！";
        }

        public static string UpdateMethod(this SampleMethod method, CoreLimsContext dbContext)
        {
            SampleMethod Method = SAMPLE.Method.getMethod(dbContext, method.Id);
            Method.Copy(method);
            dbContext.Update(Method);
            dbContext.SaveChanges();
            return "方法信息更新成功！";
        }

        public static string DeleteMethod(this SampleMethod method, CoreLimsContext dbContext, string Id)
        {
            SampleMethod Method = SAMPLE.Method.getMethod(dbContext, Id);

            // 删除方法时，应检查是否有测试关联该方法，有则不能删除。
            var trCount = dbContext.TestRelation.Where(s => s.MethodId == Method.Id).ToList().Count;
            if (trCount > 0)
            {
                var tdId = dbContext.TestRelation.Where(s => s.MethodId == Method.Id).FirstOrDefault().TestDefinitionId;
                var tdName = SAMPLE.TestDefinition.getTestDefinition(dbContext, tdId).TestName;
                return "当前方法与 " + trCount.ToString() + "个 测试相关联，第一个测试名称：" + tdName;
            }

            // 删除方法时，在所有“登录”和“检测中”的样品里查找该方法的引用，如果有引用，则不能删除。
            var name = SAMPLE.Sample.CheckHasMethod(dbContext, Method.Id);
            if (name != null)
                return "当前方法与样品 " + name + " 关联，若要删除方法，请完成所有关联样品！";

            dbContext.Remove(Method);
            dbContext.SaveChanges();
            return "删除成功！";
        }

        public static string UpdateMethodEnable(this SampleMethod method, CoreLimsContext dbContext, string Id, string Enable)
        {
            SampleMethod Method = SAMPLE.Method.getMethod(dbContext, Id);

            if (Enable == "true")
                Method.Enable = true;
            else
                Method.Enable = false;

            dbContext.Update(Method);
            dbContext.SaveChanges();
            return "状态更新成功！";
        }

        public static string[] GetRecordSheet(this SampleMethod method, CoreLimsContext dbContext, string Id = null)
        {
            var path = dbContext.Global.Where(s => s.KeyName == "RecordSheet_Path").FirstOrDefault().KeyValue;
            var files = Directory.GetFiles(path, "*.cshtml");

            var temp = new string[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                var st = files[i].Split('\\');
                temp[i] = st[st.Length - 1];
            }

            return temp;
        }

        /// <summary>
        /// 复制记录内容，ID不复制
        /// </summary>
        /// <param name="supplier"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        private static SampleMethod Copy(this SampleMethod method, SampleMethod other)
        {
            method.Code = other.Code;
            method.Name = other.Name;
            method.Type = other.Type;
            method.Description = other.Description;
            method.EffectiveDate = other.EffectiveDate;
            method.ExpireDate = other.ExpireDate;
            method.RecordSheet = other.RecordSheet;
            method.Version = other.Version;

            return method;
        }

    }
}
